package com.tonghe.service.user;

import com.tonghe.dao.BaseDao;
import com.tonghe.dao.user.UserDao;
import com.tonghe.dao.user.UserDaoImpl;
import com.tonghe.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Date:2021/12/2- 12- 02- 21:01
 **/
public class UserServiceImpl implements UserService {
    //业务层都会调用Dao层，所以要引入dao层

    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    @Override
    public User login(String userCode, String password) {//service层，专注于处理业务！！
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return  user;
    }

    @Override
    public boolean updatePwd(int id, String pwd) {//根据用户id更改密码


        System.out.println("update"+pwd);



        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            if(userDao.updatePwd(connection,id,pwd) > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //查询记录数
    @Override
    public int getUserCount(String username, int userRole) {

        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection,username,userRole);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return  count;

    }

    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {

        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

    @Override
    public boolean add(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务
            int updateRows = userDao.add(connection,user);
            connection.commit();
            if(updateRows > 0){
                flag = true;
                System.out.println("add uesr success");
            }else{
                System.out.println("add use failed");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
                System.out.println("rollback from add user");
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return flag;
    }

//    @Test
//    public void testAdd(){
//        UserServiceImpl userService = new UserServiceImpl();
//        User user1 = new User(33,"test","test","test",0,"2021-12-01","北京市海淀区成府路220号","2021-12-01",3,1,"2021-07-21 12:55:29",1,"2021-07-21 11:49:44");
//        boolean admin = userService.add(user1);
//        System.out.println(admin);
//    }

    @Test
    public void test2(){
        UserServiceImpl userService = new UserServiceImpl();
        int admin = userService.getUserCount(null,0);
        System.out.println(admin);
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        boolean admin = userService.updatePwd(1,"");
        System.out.println(admin);
    }

    @Test
    public void test1(){
        UserServiceImpl userService = new UserServiceImpl();
        User admin = userService.login("admin","admin");
        if(admin!= null) System.out.println("youren");

    }
    @Test
    public void test3() {
        int a = 20,b=5;
            System.out.println(a/b);
            System.out.println(a%b);

    }
}
